from selenium import webdriver
from selenium.webdriver import ActionChains
import time


def weibo_login(browser_drive: webdriver, loginname, password):
    """
    :param browser_drive: 传入一个webdrive
    :param loginname: 微博登录名
    :param password:登录密码
    :return: None
    """
    browser_drive.get("https://weibo.com/login.php")
    time.sleep(2)
    login_name_input_element = browser_drive.find_element_by_id('loginname')
    password_input_element = browser_drive.find_element_by_xpath(
        '//div[@class="info_list password"]/div[@class="input_wrap"]/input[@type="password"]')
    login_name_input_element.send_keys(loginname)
    password_input_element.send_keys(password)
    browser_drive.find_element_by_xpath(
        '//div[@class="info_list login_btn"]').click()
    time.sleep(2)


def weibo_search_user(browser_drive: webdriver, username):
    """
    :param browser_drive:传入webdrive
    :param username: 需要爬取的微博用户的用户名
    :return: None
    """
    search_bar = browser_drive.find_element_by_xpath(
        "//input[@node-type='searchInput']")
    search_bar.send_keys(username)
    browser_drive.find_element_by_xpath(
        "//a[@node-type='searchSubmit']").click()
    time.sleep(2)
    browser_drive.find_element_by_xpath(
        "//a[@class='name']").click()
    time.sleep(3)
    browser_drive.switch_to.window(browser_drive.window_handles[-1])


def get_weiboid(browser_driver: webdriver):
    """
    :param browser_driver:传入一个webdrive
    :return: 返回用户的微博ID号
    """
    btn = browser_driver.find_element_by_xpath(
        "//div[@class='PCD_person_info']//a[@class='WB_cardmore S_txt1 S_line1 clearfix']")
    btn.click()
    time.sleep(2)
    current_url = str(browser_driver.current_url)
    weiboid = str(current_url.split("/")[-2][6:-1])
    print(weiboid)
    browser_driver.back()
    time.sleep(2)
    return weiboid


def get_weibo_text(browser_drive: webdriver, result_file_path, username, max_page):
    """
    :param browser_drive:传入一个webdrive
    :param result_file_path: 爬取结果存储的地址
    :param username: 爬取微博的用户名
    :param max_page: 该用户全部微博的页数
    :return: None
    """
    browser_drive.find_element_by_xpath(
        "//li[@node-type='tab_all']").click()
    time.sleep(2)
    page_number = 1
    with open(result_file_path, "w") as f:
        while page_number <= max_page:
            click_number = 1
            while click_number <= 3:
                js = "window.scrollTo(0,document.body.scrollHeight)"
                browser_drive.execute_script(js)
                click_number += 1
                time.sleep(2)
            btn = browser_drive.find_elements_by_xpath(
                "//div[@class='WB_text W_f14'][@nick-name='" + username + "']//a[@action-type='fl_unfold']")
            for b in btn:
                ActionChains(browser_drive).move_to_element(b).perform()
                time.sleep(1)
                browser_drive.execute_script('arguments[0].click()', b)
            js = "window.scrollTo(0,document.body.scrollHeight)"
            browser_drive.execute_script(js)
            weiboText = browser_drive.find_elements_by_xpath(
                "//div[@class='WB_text W_f14'][@nick-name='" + username + "']")
            for t in weiboText:
                f.write(t.text + "\n")
                print(t.text)
            if page_number <= max_page - 1:
                browser_drive.find_element_by_xpath(
                    "//div[@node-type='feed_list_page']//a[@class='page next S_txt1 S_line1']").click()
                time.sleep(2)
            else:
                break
            page_number += 1


# 例子
browser = webdriver.Chrome()
weibo_login(browser, "登录名", "登录密码")
weibo_search_user(browser, "搜索的用户名")
get_weibo_text(browser, "resulttest.txt", "搜索的用户名", 34)
browser.quit()

